﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.OracleClient;
using Xiaov.SmartWeb.Application.Models;

namespace Xiaov.SmartWeb.Application.SZBBTree.NH08
{
  public partial class InitTable : System.Web.UI.Page
  {
    public string tableName = "", nf = "", yf = "", dwbm = "";
    XMLVERSION modelXml = null;

    protected void Page_Load(object sender, EventArgs e)
    {
      if (Request.QueryString["tableName"] != null)
      { tableName = Request.QueryString["tableName"].ToString(); }
      if (Request.QueryString["nf"] != null)
      { nf = Request.QueryString["nf"].ToString(); }
      if (Request.QueryString["yf"] != null)
      { yf = Request.QueryString["yf"].ToString(); }
      if (Request.QueryString["dwbm"] != null)
      { dwbm = Request.QueryString["dwbm"].ToString(); }
      CreateReport();
      //InitVersion();
      modelXml = new XMLVERSION(nf, yf, dwbm, tableName);
      string xml = GetXml();
      Response.ContentType = "text/xml";
      ////关键3：告诉浏览器，这是数据，就不要缓存我了
      Response.CacheControl = "no-cache";
      Response.Write(xml);
      Response.End();
    }

    protected string GetXml()
    {
      XmlDocument doc = new XmlDocument();
      string path = Server.MapPath("./") + "//" + tableName + ".xml";
      doc.Load(path);
      XmlNode report = doc.DocumentElement;
      DataTable dt = GetDataXml().Tables[0];
      for (int i = 0; i < dt.Rows.Count; i++)
      {
        string zbbm = dt.Rows[i]["zbbm"].ToString();
        string value1 = dt.Rows[i]["col51"].ToString();
        string value2 = dt.Rows[i]["col52"].ToString();
        XmlNode node1 = report.SelectSingleNode("//TD[@alias='" + zbbm + "_COL51']");
        node1.InnerText = value1;
        XmlNode node2 = report.SelectSingleNode("//TD[@alias='" + zbbm + "_COL52']");
        node2.InnerText = value2;
      }
      string xml = doc.OuterXml;
      return xml;
    }

    private DataSet GetDataXml()
    {
      StringBuilder strSql = new StringBuilder();
      strSql.Append("select ");
      strSql.Append(modelXml.SELECTFIELD);
      strSql.Append(" FROM  ");
      strSql.Append(tableName);
      strSql.Append(" where dwbm=:0 and nf=:1 and yf=:2 order by hc");
      OracleParameter[] parms = { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20),
                                new OracleParameter("2",OracleType.VarChar,10)};
      parms[0].Value = dwbm;
      parms[1].Value = nf;
      parms[2].Value = yf;
      DataSet ds = DbHelperOra.Query(strSql.ToString(), parms);
      foreach (DataRow dr in ds.Tables[0].Rows)
      {
        string value = dr["ZBMC"].ToString();
        int cj = int.Parse(dr["cj"].ToString());
        for (int cjInd = 1; cjInd < cj; cjInd++)
        {
          value = "  " + value;
        }
        dr["ZBMC"] = value;
      }
      return ds;
    }

    private void CreateReport()
    {
      if (!IsExist())
      {
        LoadProc("p_create");
      }
    }

    private bool IsExist()
    {
      return Maticsoft.DBUtility.DbHelperOra.Exists("select Count(1) from " + tableName
        + " where dwbm='" + dwbm + "' and nf='" + nf + "' and yf='" + yf + "'");
    }

    public bool LoadProc(string ProcdureName)
    {
      OracleParameter[] parameters ={
            new OracleParameter("v_bbbm",OracleType.VarChar,20),
　　     new OracleParameter("v_dwbm",OracleType.VarChar,20),
　　     new OracleParameter("v_nf",OracleType.Number,4),
            new OracleParameter("v_yf",OracleType.Number,2),
　　      };
      parameters[0].Value = tableName.ToUpper();
      parameters[1].Value = dwbm.ToUpper();
      parameters[2].Value = nf;
      parameters[3].Value = yf;
      try
      {
        OracleDataReader odr = Maticsoft.DBUtility.DbHelperOra.RunProcedure(ProcdureName, parameters); odr.Close();
        return true;
      }
      catch
      {
        return false;
      }
    }
  }
}